package baseFx.tests.common.lock;

import baseFx.common.AutoCloseable;
import baseFx.common.lock.AppLockspace;
import baseFx.common.lock.Lockspace;
import baseFx.common.lock.LockspaceException;
import org.junit.Assert;
import org.junit.Test;

import java.util.UUID;

public class ExLockspaceTest {
    @Test
    public void 锁定失败直接抛出异常() {
        String key = UUID.randomUUID().toString();
        int status = 0;
        Lockspace lockspace = getLockspace();
        AutoCloseable ac = null;
        try {
            ac = lockspace.exLock(key);
            status++;
        } catch (LockspaceException e) {
            e.printStackTrace();
        }
        try {
            lockspace.exLock(key);
            status = -1;
        } catch (LockspaceException e) {
            status++;
        }
        try {
            ac.close();
            lockspace.exLock(key);
            status++;
        } catch (LockspaceException e) {
            status = -1;
        }
        Assert.assertEquals(status, 3);
    }

    protected Lockspace getLockspace() {
        return new AppLockspace();
//        return new RedisLockspace(new JedisPool());
    }
}
